<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDept('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-divider />
        <!-- 树形控件 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title="isEdit? '编辑部门':'添加部门'" :visible.sync="showDialog">
        <DeptDialog v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @success="success" @cancle="cancle" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { transArray } from '@/utils'
import DeptDialog from './deptDialog.vue'
export default {
  name: '',
  components: { DeptDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  computed: {},
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartments()
      this.list = transArray(res.data.depts)
      console.log(res.data, '获取组织架构信息')
    },
    addDept(id) {
      this.showDialog = true
      this.isEdit = false
      this.curId = id
    },
    editDept(id) {
      this.showDialog = true
      this.isEdit = true
      this.curId = id
    },
    // 删除部门
    async delDept(id) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartment(id)
        this.$message.success(res.message)
        this.loadDepartments()
      }
      ).catch(e => e)
    },
    success() {
      this.showDialog = false
      this.loadDepartments()
    },
    cancle() {
      this.showDialog = false
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
